package com.ideaaedi.zoo.example.springboot.litefeature.cs.config;

import cn.hutool.extra.servlet.JakartaServletUtil;
import com.ideaaedi.commonspring.lite.params.ParameterRecorderCustomizer;
import com.ideaaedi.commonspring.lite.params.RequestPathProvider;
import com.ideaaedi.zoo.commonbase.util.ZooContext;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.lang.reflect.Method;

/**
 * (non-javadoc)
 *
 * @author <font size = "20" color = "#3CAA3C"><a href="https://gitee.com/JustryDeng">JustryDeng</a></font> <img
 * src="https://gitee.com/JustryDeng/shared-files/raw/master/JustryDeng/avatar.jpg" />
 * @since 1.0.0
 */
@Configuration
public class DemoParameterConfig {
    
    /**
     * 在请求入口时，同时记录url 等信息
     * <pre>
     * 更多自定义配置详见{@link ParameterRecorderCustomizer}
     * </pre>
     */
    @Bean
    public RequestPathProvider demoRequestPathProvider() {
        return new RequestPathProvider() {
            @Override
            public String requestPath(Method method) {
                HttpServletRequest httpServletRequest = ZooContext.Http.httpServletRequest();
                if (httpServletRequest == null) {
                    return null;
                }
                return httpServletRequest.getServletPath() + " | " + JakartaServletUtil.getClientIP(httpServletRequest)
                        + " | " + httpServletRequest.getHeader("User-Agent");
            }
        };
    }
}
